<template>
  <el-dialog
    :close-on-click-modal="false"
    :visible.sync="dialog"
    :title="isAdd?'新增子节点':'编辑子节点'"
    width="50%"
  >
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
      <el-form-item label="字典名称" prop="supplement">
        <el-input v-model="form.supplement"/>
      </el-form-item>
      <el-form-item label="字典标签" prop="name">
        <el-input v-model="form.name" :disabled="!isAdd"/>
      </el-form-item>
      <el-form-item label="字典值">
        <el-tabs v-model="activeName" type="card" @tab-click="tabActive">
          <el-tab-pane label="Text" name="first">
            <el-input
              v-model="form.value"
              type="textarea"
              :row="5"
              autosize
            />
          </el-tab-pane>
          <!-- <el-tab-pane label="富文本" name="second">
            <tinymce v-model="form.value"/>
          </el-tab-pane>
          <el-tab-pane label="CodeEditor" name="third">
            <codemirror ref="codeRef" v-model="form.value" :options="options"/>
          </el-tab-pane> -->
        </el-tabs>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { dictionary_add, dictionary_update } from '@/api/system';
export default {
  props: {
    isAdd: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      loading: false,
      dialog: false,
      activeName: 'first',
      form: {
        id: null,
        name: '',
        value: '--',
        supplement: '',
        parentId: 1
      },
      rules: {
        supplement: [{ required: true, message: '请输入字典名称', trigger: 'blur' }],
        name: [{ required: true, message: '请输入字典标签', trigger: 'blur' }]
      }
    };
  },
  computed: {

    options() {
      return {
        theme: 'darcula',
        tabSize: 2,
        mode: 'javascript',
        styleActiveLine: false,
        matchBrackets: true,
        // autofocus: true,
        lineNumbers: true,
        lineWrapping: true,
        highlightDifferences: true,
        dragDrop: true,
        extraKeys: { 'Ctrl-Space': 'autocomplete' }
      };
    }
  },
  methods: {
    tabActive(val) {
      if (val.label === 'CodeEditor') {
        this.$refs.codeRef.codemirror.focus();
        this.$nextTick(() => {
          this.$refs.codeRef.codemirror.refresh();
        });

        console.log(this.$refs.codeRef.codemirror);
        // this.$refs.codeRef.codemirror._handlers.refresh[0]();
      }
    },
    cancel() {
      this.dialog = false;
      this.resetForm();
    },
    doSubmit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.isAdd) {
            this.doAdd();
          } else this.doEdit();
        }
      });
    },
    async doAdd() {
      try {
        await dictionary_add(this.form);
        this.resetForm();
        this.$message({
          message: '添加成功',
          type: 'success'
        });
        this.loading = false;
        this.dialog = false;
        this.$parent.getData();
      } catch (error) {
        console.error(error);
        this.loading = false;
      }
    },
    async doEdit() {
      try {
        await dictionary_update(this.form);
        this.resetForm();
        this.$message({
          message: '修改成功',
          type: 'success'
        });
        this.loading = false;
        this.dialog = false;
        this.$parent.getData();
      } catch (error) {
        console.error(error);
        this.loading = false;
      }
    },
    resetForm() {
      this.form = {
        id: null,
        name: '',
        value: '_待编辑',
        supplement: '',
        parentId: 1
      };
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
/deep/ .el-input-number .el-input__inner {
  text-align: left;
}
</style>
